import json

from db import MySQLDatabase
from selenium import webdriver
from selenium.webdriver.common.by import By

# 获取数据库连接
def get_db(host, user, password, database):
    return MySQLDatabase(host, user, password, database)
# 采集数据
def get_data():
    data_list = []
    chrome_options = webdriver.ChromeOptions()
    chrome_options.add_argument('--headless')
    chrome_options.add_argument('--disable-gpu')
    chrome_options.add_argument('--no-sandbox')
    chrome_options.add_argument('--window-size=1700x938')
    client = webdriver.Chrome(options=chrome_options)
    client.get("https://www.cwl.gov.cn/ygkj/wqkjgg/kl8/")
    rows = client.find_elements(By.XPATH, '/html/body/div[4]/div/div/div[2]/div/div[1]/div[3]/table/tbody/tr')
    for row in rows:
        data_list.append((int(row.find_element(By.XPATH, './td[1]').text), json.dumps(row.find_element(By.XPATH, './td[3]/div').text.split())))
    client.quit()
    return data_list

# 判断数据库中是否存在该数据，不存在返回false，存在返回true
def check_exist(db, sql, data):
    return db.check_exist(sql, data)

def main():
    db = get_db("10.187.49.188", "admin", "123456", "ruoyi-fastapi")
    datalist= get_data()
    if len(datalist)>0 and not check_exist(db, "SELECT * FROM kuaile8 WHERE qi = %s", [datalist[0][0]]):
        # for item in get_data():
        #     db.create("INSERT INTO kuaile8 (qi, zj_number) VALUES (%s, %s)", item)

        # print(datalist[0][0])
        # print(not check_exist(db, "SELECT * FROM kuaile8 WHERE qi = %s", [datalist[0][0]]))

        db.create("INSERT INTO kuaile8 (qi, zj_number) VALUES (%s, %s)", datalist[0])

if __name__ == "__main__":
    main()
